import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.Skills;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class AllSmither extends Script implements PaintListener,
		ServerMessageListener {

	// bar id's
	int bbarid = 2349;
	int ibarid = 2351;
	int sbarid = 2353;
	int mbarid = 2359;
	int abarid = 2361;
	int rbarid = 2363;

	// number of bars
	int dagger = 1;
	int axe = 1;
	int mace = 1;
	int med = 1;
	int sword = 1;
	int scim = 2;
	int lsword = 2;
	int full = 2;
	int sqshield = 2;
	int hammer = 3;
	int baxe = 3;
	int cbody = 3;
	int kshield = 3;
	int h2h = 3;
	int legs = 3;
	int skirt = 3;
	int body = 5;
	// new
	int crossbolts = 1;
	int nails = 1;
	int studs = 1;// Also Wire
	int arrowtips = 1;
	int crosslimbs = 1;
	int lantern = 1;
	int trowknife = 1;
	int grapple = 1;
	int pick = 2;
	int claws = 2;

	// coords x
	int x1l = 15;
	int x1r = 120;
	int x2l = 132;
	int x2r = 230;
	int x3l = 250;
	int x3r = 350;
	int x4l = 380;
	int x4r = 480;

	// coords y
	int y1l = 75;
	int y1r = 80;
	int y2l = 125;
	int y2r = 130;
	int y3l = 175;
	int y3r = 180;
	int y4l = 225;
	int y4r = 230;
	int y5l = 275;
	int y5r = 280;

	// item y
	int ydagger = 1;
	int yaxe = 1;
	int ymace = 1;
	int ymed = 1;
	int ysword = 2;
	int yscim = 3;
	int ylsword = 4;
	int yfull = 4;
	int ysqshield = 4;
	int yhammer = 5;
	int ybaxe = 5;
	int ycbody = 4;
	int ykshield = 4;
	int yh2h = 4;
	int ylegs = 5;
	int yskirt = 5;
	int ybody = 5;
	// new
	int ycrossbolts = 2;
	int ynails = 2;
	int ystuds = 3;// also Wire
	int yarrowtips = 3;
	int ycrosslimbs = 3;
	int ylantern = 5;
	int ytrowknife = 4;
	int ygrapple = 1;
	int ypick = 5;
	int yclaws = 4;

	// item x
	int xdagger = 1;
	int xaxe = 2;
	int xmace = 3;
	int xmed = 4;
	int xsword = 2;
	int xscim = 3;
	int xlsword = 1;
	int xfull = 3;
	int xsqshield = 4;
	int xhammer = 3;
	int xbaxe = 4;
	int xcbody = 1;
	int xkshield = 2;
	int xh2h = 4;
	int xlegs = 2;
	int xskirt = 1;
	int xbody = 3;
	// new
	int xcrossbolts = 1;
	int xnails = 4;
	int xstuds = 1;// also Wire
	int xarrowtips = 2;
	int xcrosslimbs = 4;
	int xlantern = 1;
	int xtrowknife = 2;
	int xgrapple = 2;
	int xpick = 4;
	int xclaws = 3;

	// item s
	int sdagger = 1;
	int saxe = 1;
	int smace = 1;
	int smed = 1;
	int ssword = 1;
	int sscim = 1;
	int slsword = 1;
	int sfull = 1;
	int ssqshield = 1;
	int shammer = 1;
	int sbaxe = 1;
	int scbody = 2;
	int skshield = 2;
	int sh2h = 2;
	int slegs = 2;
	int sskirt = 2;
	int sbody = 2;
	// new
	int scrossbolts = 1;
	int snails = 1;
	int sstuds = 1;// also Wire
	int sarrowtips = 1;
	int scrosslimbs = 1;
	int slantern = 1;
	int strowknife = 1;
	int sgrapple = 2;
	int spick = 2;
	int sclaws = 2;

	// other settings
	int hammerid = 2347;
	int bars = 0;
	int status = 1;
	int bankid = 11402;
	int anvilid = 2783;
	int type = 0;
	int animation = 898;
	int random = 0;
	int usingbar = 0;
	int usingneed = 0;
	int cam = 0;
	int xxx = 0;
	int yyy = 0;
	int sss = 0;
	int xxl = 0;
	int xxr = 0;
	int yyl = 0;
	int yyr = 0;
	int xphad = 0;
	int xpGained = 0;
	int maked = 0;
	int index = 0;
	int banked = 0;
	public String statusp = "";
	public String barstr = "";
	public String itemstr = "";
	public long startTime = System.currentTimeMillis();
	public int startexp;

	public RSTile[] bank2anvil = new RSTile[] { new RSTile(3186, 3432),
			new RSTile(3185, 3431), new RSTile(3187, 3428),
			new RSTile(3186, 3425) };

	public RSTile[] anvil2bank = new RSTile[] { new RSTile(3185, 3428),
			new RSTile(3185, 3431), new RSTile(3185, 3424),
			new RSTile(3185, 3430), new RSTile(3185, 3435) };

	public boolean anvilo() {
		while (!RSInterface.getInterface(300).isValid()) {// while isn't done
			if (getSelectedItemID() == usingbar) {// if bar selected
				// nothing in here
			} else {// if not selected
				atInventoryItem(usingbar, "Use");
			}
			waiting();
			final RSObject anvil = findObject(anvilid);

			if (anvil == null) {
				log("Did not find anvil.");
				return false;
			}
			rott();
			waiting();
			atObject(anvil, "Anvil");
			waiting();
		}
		return true;
	}

	public boolean deposit() {
		if (!bank.isOpen()) {
			return false;
		}
		bank.depositAllExcept(hammerid);

		if (!isCarryingItem(hammerid)) {
			bank.atItem(hammerid, "Withdraw-1");

			waitingl();
		}
		if (type == 1) {
			bank.atItem(bbarid, "Withdraw-All");
		}
		if (type == 2) {
			bank.atItem(ibarid, "Withdraw-All");
		}
		if (type == 3) {
			bank.atItem(sbarid, "Withdraw-All");
		}
		if (type == 4) {
			bank.atItem(mbarid, "Withdraw-All");
		}
		if (type == 5) {
			bank.atItem(abarid, "Withdraw-All");
		}
		if (type == 6) {
			bank.atItem(rbarid, "Withdraw-All");
		}

		waiting();
		bank.close();
		return true;
	}

	public String getAuthor() {
		return "ikkentim";
	}

	public String getName() {
		return "All Smither";
	}

	public String getScriptCategory() {
		return "Smithing";
	}

	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<head></head>";
		html += "<center><h2>" + getName() + "</h2>" + "</center>";
		html += "<b>Author:</b> " + getAuthor() + "<br />";
		html += "<b>Version:</b> " + getVersion() + "<br />";
		html += "Start in W-Varrock bank<br />";
		html += "You <u>must</u> see the bars if you just open the bank.<br />";
		html += "You <u>also must</u> see the hammer if you just open the bank,<br />or you have the hammer in your pocket.<br />";
		html += "Type: A <select name=\"type\"><option>Bronze<option>Iron<option>Steel<option>Mithril<option>Adamant<option>Rune</select> ";
		html += " <select name=\"what\"><option>Dagger<option>Axe<option>Medium Helm<option>Sword<option>Scimitar<option>Longsword<option>Full helm<option>Square shield<option>Warhammer<option>Battleaxe<option>Chainbody<option>Kiteshield<option>2-H Sword<option>Platelegs<option>Plateskirt<option>Platebody";
		html += "<option>Crossbow bolts<option>Nails<option>Studs<option>Wire<option>Iron split<option>Arrowtips<option>Crossbow limbs<option>Bullseye lantern<option>Oil lantern<option>Trowing knife<option>Grapple tip<option>Claws<option>Pickaxe<option>Claws</select><br />";
		html += "Number of bars: <input type=text name=bars value=\"0\">";
		html += "</html>";
		return html;
	}

	public double getVersion() {
		return 1.02;
	}

	public int loop() {
		if (RSInterface.getInterface(109).isValid()) {// if Collection Box is
			// opend
			// this is a debug, cuz sometimes he opens Collection Box.
			// i got this from The anti-Random 'Interface Closer', made by
			// 'HeyyamaN'.
			// this is the original, tnx to 'HeyyamaN'!
			atInterface(RSInterface.getInterface(109).getChild(13)); // GE
			// Collection
			// Box
			wait(random(500, 900));
		}
		if (bars < usingneed) {
			walkPathMM(randomizePath(anvil2bank, 7, 7), 15);
			log("Out of bars.");
			waiting();
			stopping();
		}
		if (!getMyPlayer().isMoving()) {// if not walking
			if (distanceTo(new RSTile(3186, 3425)) < 2) {// if @anvil
				status = 4;
			}
			if (distanceTo(new RSTile(3185, 3435)) < 3) {// if @bank
				status = 1;
			}
			if (status == 1) {// open bank
				statusp = "Searching for a bank booth...";
				openBank();
				waiting();
				while (!bank.isOpen()) {
					if (RSInterface.getInterface(109).isValid()) {// if
						// Collection
						// Box is
						// opend
						// this is a debug, cuz sometimes he opens Collection
						// Box.
						// i got this from The anti-Random 'Interface Closer',
						// made by 'HeyyamaN'.
						// this is the original, tnx to 'HeyyamaN'!
						atInterface(RSInterface.getInterface(109).getChild(13)); // GE
						// Collection
						// Box
						wait(random(500, 900));
					}
					openBank();
					waiting();
				}
				status = 2;
			}
			if (status == 2) {// deposit and withdraw
				if (bank.isOpen()) {
					statusp = "Banking...";
					deposit();
					banked = 1;
					status = 3;
					waiting();
					if (getInventoryCount(usingbar) < usingneed) {
						deposit(); // retry
						if (getInventoryCount(usingbar) < usingneed) {
							log("Out of bars.");
							waiting();
							stopping();
						}
					}
					if (bars < usingneed) {
						log("Out of bars.");
						waiting();
						stopping();
					}
				} else {
					statusp = "Searching for a bank booth...";
					openBank();
					waiting();
				}
			}
			if (status == 3) {// walk2anvil
				statusp = "Walking to Anvil...";
				setRun(true);
				walkPathMM(randomizePath(bank2anvil, 1, 1), 15);
				waitingl();
				return 0;
			}
			if (status == 4) {// open anvil
				statusp = "Searching for Anvil...";
				anvilo();
				if (RSInterface.getInterface(300).isValid()) {
					status = 5;
				}
			}
			if (status == 5) {// select anvil
				statusp = "Smithing...";
				if (getMyPlayer().getAnimation() == animation) {// if smithing
					waitingl();
					return 2000;
				} else {// if not smithing
					waitingk();
					if (getMyPlayer().getAnimation() == animation) {// if
						// smithing
						return 2000;
					} else {
						if (banked == 1) {
							sellectanvil();
							banked = 0;
						} else {
							status = 9;
						}
					}
				}
				return 5000;
			}
			if (status == 9) {// wait, walk2bank
				if (bars < usingneed) {
					walkPathMM(randomizePath(anvil2bank, 2, 2), 15);
					log("Out of bars.");
					waiting();
					stopping();
					return -1;
				}
				if (getInventoryCount(usingbar) < usingneed) {
					statusp = "Walking to bank...";
					setRun(true);
					walkPathMM(randomizePath(anvil2bank, 2, 2), 15);
					waitingl();
				}
				return 0;
			}

		}
		return 5000;
	}

	public void onFinish() {
		log("Stopping Script...");
		log("Tnx for using my script!");
		Bot.getEventManager().removeListener(ServerMessageListener.class, this);
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {

		if (isLoggedIn()) {// if logged in

			// set xp
			index = Skills.getStatIndex("smithing");
			xphad = xpGained;
			xpGained = skills.getCurrentSkillExp(index) - startexp;

			// set time
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;
			// set title
			g.setFont(new Font("Comic Sans", Font.BOLD, 17));
			g.setColor(Color.blue);
			g.drawString("IkKenTim's AllSmither.", 300, 27);

			// check bars
			if (bars < usingneed) {
				walkPathMM(randomizePath(anvil2bank, 4, 4), 15);
				log("Out of bars.");
				waiting();
				stopping();

			}
			// set paint
			g.setFont(new Font("Comic Sans", Font.BOLD, 12));
			g.setColor(Color.red);
			g.drawString("Time running: " + hours + ":" + minutes + ":"
					+ seconds + ".", 300, 40);
			g.drawString("Bars left : " + bars, 300, 55);
			g.drawString(itemstr + " maked: " + maked, 300, 70);
			g.drawString("Status: " + statusp, 300, 85);
			g.drawString("XP Gained: " + xpGained, 300, 100);
			g.setFont(new Font("Comic Sans", Font.BOLD, 9));
		}

	}

	public boolean onStart(final Map<String, String> args) {
		log("AllSmither by ikkentim.");
		if (!isLoggedIn()) {
			log("You must be logged in to run this script!");
			return false;
		} else {

			startTime = System.currentTimeMillis();
			index = Skills.getStatIndex("smithing");
			startexp = skills.getCurrentSkillExp(index);

			if (getMyPlayer().getLocation().getX() > 3178
					&& getMyPlayer().getLocation().getY() < 3447
					&& getMyPlayer().getLocation().getX() < 3195
					&& getMyPlayer().getLocation().getY() > 3430) {

				// set type
				if (args.get("type").equals("Bronze")) {
					type = 1;
					usingbar = bbarid;
				}
				if (args.get("type").equals("Iron")) {
					type = 2;
					usingbar = ibarid;
				}
				if (args.get("type").equals("Steel")) {
					type = 3;
					usingbar = sbarid;
				}
				if (args.get("type").equals("Mithril")) {
					type = 4;
					usingbar = mbarid;
				}
				if (args.get("type").equals("Adamant")) {
					type = 5;
					usingbar = abarid;
				}
				if (args.get("type").equals("Rune")) {
					type = 6;
					usingbar = rbarid;
				}
				barstr = args.get("type");
				bars = Integer.parseInt(args.get("bars"));

				// set what
				if (args.get("what").equals("Dagger")) {
					usingneed = dagger;
					xxx = xdagger;
					yyy = ydagger;
					sss = sdagger;
				}
				if (args.get("what").equals("Axe")) {
					usingneed = axe;
					xxx = xaxe;
					yyy = yaxe;
					sss = saxe;
				}
				if (args.get("what").equals("Medium Helm")) {
					usingneed = med;
					xxx = xmed;
					yyy = ymed;
					sss = smed;
				}
				if (args.get("what").equals("Sword")) {
					usingneed = sword;
					xxx = xsword;
					yyy = ysword;
					sss = ssword;
				}
				if (args.get("what").equals("Scimitar")) {
					usingneed = scim;
					xxx = xscim;
					yyy = yscim;
					sss = sscim;
				}
				if (args.get("what").equals("Longsword")) {
					usingneed = lsword;
					xxx = xlsword;
					yyy = ylsword;
					sss = slsword;
				}
				if (args.get("what").equals("Full helm")) {
					usingneed = full;
					xxx = xfull;
					yyy = yfull;
					sss = sfull;
				}
				if (args.get("what").equals("Square shield")) {
					usingneed = sqshield;
					xxx = xsqshield;
					yyy = ysqshield;
					sss = ssqshield;
				}
				if (args.get("what").equals("Warhammer")) {
					usingneed = hammer;
					xxx = xhammer;
					yyy = yhammer;
					sss = shammer;
				}
				if (args.get("what").equals("Battleaxe")) {
					usingneed = baxe;
					xxx = xbaxe;
					yyy = ybaxe;
					sss = sbaxe;
				}
				if (args.get("what").equals("Chainbody")) {
					usingneed = cbody;
					xxx = xcbody;
					yyy = ycbody;
					sss = scbody;
				}
				if (args.get("what").equals("Kiteshield")) {
					usingneed = kshield;
					xxx = xkshield;
					yyy = ykshield;
					sss = skshield;
				}
				if (args.get("what").equals("2-H Sword")) {
					usingneed = h2h;
					xxx = xh2h;
					yyy = yh2h;
					sss = sh2h;
				}
				if (args.get("what").equals("Platelegs")) {
					usingneed = legs;
					xxx = xlegs;
					yyy = ylegs;
					sss = slegs;
				}
				if (args.get("what").equals("Plateskirt")) {
					usingneed = skirt;
					xxx = xskirt;
					yyy = yskirt;
					sss = sskirt;
				}
				if (args.get("what").equals("Platebody")) {
					usingneed = body;
					xxx = xbody;
					yyy = ybody;
					sss = sbody;
				}
				// members under here
				if (args.get("what").equals("Crossbow bolts")) {
					usingneed = crossbolts;
					xxx = xcrossbolts;
					yyy = ycrossbolts;
					sss = scrossbolts;
				}
				if (args.get("what").equals("Nails")) {
					usingneed = nails;
					xxx = xnails;
					yyy = ynails;
					sss = snails;
				}
				if (args.get("what").equals("Studs")
						&& args.get("type").equals("Steel")) {
					usingneed = studs;
					xxx = xstuds;
					yyy = ystuds;
					sss = sstuds;
				} else if (args.get("what").equals("Studs")) {
					log("Combination of Bar and Item not posible.");
					return false;
				}
				if (args.get("what").equals("Wire")
						&& args.get("type").equals("Bronze")) {
					usingneed = studs;
					xxx = xstuds;
					yyy = ystuds;
					sss = sstuds;
				} else if (args.get("what").equals("Wire")) {
					log("Combination of Bar and Item not posible.");
					return false;
				}
				if (args.get("what").equals("Iron split")
						&& args.get("type").equals("Iron")) {
					usingneed = studs;
					xxx = xstuds;
					yyy = ystuds;
					sss = sstuds;
				} else if (args.get("what").equals("Iron split")) {
					log("Combination of Bar and Item not posible.");
					return false;
				}
				if (args.get("what").equals("Arrowtips")) {
					usingneed = arrowtips;
					xxx = xarrowtips;
					yyy = yarrowtips;
					sss = sarrowtips;
				}
				if (args.get("what").equals("Crossbow limbs")) {
					usingneed = crosslimbs;
					xxx = xcrosslimbs;
					yyy = ycrosslimbs;
					sss = scrosslimbs;
				}
				if (args.get("what").equals("Bullseye lantern")
						&& args.get("type").equals("Steel")) {
					usingneed = lantern;
					xxx = xlantern;
					yyy = ylantern;
					sss = slantern;
				} else if (args.get("what").equals("Bullseye lantern")) {
					log("Combination of Bar and Item not posible.");
					return false;
				}
				if (args.get("what").equals("Oil lantern")
						&& args.get("type").equals("Iron")) {
					usingneed = lantern;
					xxx = xlantern;
					yyy = ylantern;
					sss = slantern;
				} else if (args.get("what").equals("Oil lantern")) {
					log("Combination of Bar and Item not posible.");
					return false;
				}
				if (args.get("what").equals("Trowing knife")) {
					usingneed = trowknife;
					xxx = xtrowknife;
					yyy = ytrowknife;
					sss = strowknife;
				}
				if (args.get("what").equals("Grapple tip")
						&& args.get("type").equals("Mithril")) {
					usingneed = grapple;
					xxx = xgrapple;
					yyy = ygrapple;
					sss = sgrapple;
				} else if (args.get("what").equals("Grapple tip")) {
					log("Combination of Bar and Item not posible.");
					return false;
				}
				if (args.get("what").equals("Pickaxe")
						&& args.get("type").equals("Mithril")) {
					usingneed = pick;
					xxx = xpick;
					yyy = ypick;
					sss = spick;
				} else if (args.get("what").equals("Pickaxe")
						&& args.get("type").equals("Adamant")) {
					usingneed = pick;
					xxx = xpick;
					yyy = ypick;
					sss = spick;
				} else if (args.get("what").equals("Pickaxe")
						&& args.get("type").equals("Rune")) {
					usingneed = pick;
					xxx = xpick;
					yyy = ypick;
					sss = spick;
				} else if (args.get("what").equals("Pickaxe")) {
					log("Combination of Bar and Item not posible.");
					return false;
				}
				if (args.get("what").equals("Claws")
						&& args.get("type").equals("Mithril")) {
					usingneed = claws;
					xxx = xclaws;
					yyy = yclaws;
					sss = sclaws;
				} else if (args.get("what").equals("Claws")
						&& args.get("type").equals("Adamant")) {
					usingneed = claws;
					xxx = xclaws;
					yyy = yclaws;
					sss = sclaws;
				} else if (args.get("what").equals("Claws")
						&& args.get("type").equals("Rune")) {
					usingneed = claws;
					xxx = xclaws;
					yyy = yclaws;
					sss = sclaws;
				} else if (args.get("what").equals("Claws")) {
					log("Combination of Bar and Item not posible.");
					return false;
				}
				itemstr = args.get("what");

				// set x and y and s
				if (xxx == 1) {
					xxl = x1l;
					xxr = x1r;
				}
				if (xxx == 2) {
					xxl = x2l;
					xxr = x2r;
				}
				if (xxx == 3) {
					xxl = x3l;
					xxr = x3r;
				}
				if (xxx == 4) {
					xxl = x4l;
					xxr = x4r;
				}

				if (yyy == 1) {
					yyl = y1l;
					yyr = y1r;
				}
				if (yyy == 2) {
					yyl = y2l;
					yyr = y2r;
				}
				if (yyy == 3) {
					yyl = y3l;
					yyr = y3r;
				}
				if (yyy == 4) {
					yyl = y4l;
					yyr = y4r;
				}
				if (yyy == 5) {
					yyl = y5l;
					yyr = y5r;
				}
				setCameraAltitude(true);
				setCompass('N');
				cam = 359;
				if (args.get("bars") == null) {
					log("No number of bars set.");
					return false;
				}
				log("BarType: " + args.get("type"));
				log("What: " + args.get("what"));
				log("Bars: " + args.get("bars"));
				return true;
			} else {
				log("You have to be in the W-Varrock bank.");
				return false;
			}
		}
	}

	public boolean openBank() {
		final RSObject bank = findObject(bankid);
		if (bank == null) {
			log("Did not find bank.");
			return false;
		}
		rott();
		atObject(bank, "Use-quickly");

		return true;

	}

	public boolean rott() {
		cam += 10;
		while (cam > 359) {
			cam -= 359;
		}
		setCameraRotation(cam);
		return true;
	}

	public boolean sellectanvil() {
		clickMouse(random(495, 504), random(75, 80), 1, 1, true);
		waitingk();
		if (sss == 1) {
			clickMouse(random(495, 504), random(75, 140), 1, 1, true);
			waitingk();
		}
		if (sss == 2) {
			clickMouse(random(495, 504), random(245, 305), 1, 1, true);
			waitingk();
		}
		clickMouse(random(xxl, xxr), random(yyl, yyr), 5, 5, false);
		atMenu("Make All");
		waitingl();
		return true;
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		final String message = e.getMessage();
		if (message.contains("You make a")) {
			maked += 1;
			bars -= usingneed;
		}
		if (message.contains("You've just advanced a Smithing level!")) {
			status = 4;
		}
	}

	public boolean stopping() {
		stopAllScripts();
		return false;
	}

	public boolean waiting() {
		random = random(800, 1000);
		wait(random);
		return true;
	}

	public boolean waitingk() {
		random = random(500, 600);
		wait(random);
		return true;
	}

	public boolean waitingl() {
		random = random(1000, 1200);
		wait(random);
		return true;
	}
}